﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;
using System.IO;
using OpenPop.Mime;
using System.Drawing;
using System.Windows.Forms;
using NHunspell;

using System.Security.Cryptography;

namespace SosBL
{
    public static class Utils
    {
        /// <summary>
        /// Obtener el contraste de color
        /// </summary>
        /// <param name="color">color</param>
        /// <returns>Color</returns>
        public static Color ContrastColor(Color color)
        {
            int d = 0;
            // Counting the perceptive luminance - human eye favors green color... 
            double a = 1 - (0.299 * color.R + 0.587 * color.G + 0.114 * color.B) / 255;

            if (a < 0.5)
                d = 0; // bright colors - black font
            else
                d = 255; // dark colors - white font

            return Color.FromArgb(d, d, d);
        }

        public enum Language
        {
            catalan,
            español,
            English
        }

        /// <summary>
        /// check si palabra es correcta
        /// </summary>
        /// <param name="text">texto</param>
        /// <param name="lan">Lenguaje</param>
        /// <returns></returns>
        public static bool isWordCorrect(string text, Language lan)
        {
            Hunspell spell = null;

            switch (lan)
            {
                case Language.catalan:
                    spell = new Hunspell("catalan.aff", "catalan.dic");
                    break;
                case Language.español:
                    spell = new Hunspell("spanish.aff", "spanish.dic");
                    break;
                case Language.English:
                    spell = new Hunspell("english.aff", "english.dic");
                    break;
                default:
                    break;
            }

            return spell.Spell(text);
        }

        /// <summary>
        /// Obtener sugerencias
        /// </summary>
        /// <param name="text">Texto</param>
        /// <param name="lan">Lenguaje</param>
        /// <returns>Lista de sugerencias</returns>
        public static List<string> GetSuggestionsSpeller(string text, Language lan)
        {
            List<string> suggestions = new List<string>();

            Hunspell spell = null;

            switch (lan)
            {
                case Language.catalan:
                    spell = new Hunspell("catalan.aff", "catalan.dic");
                    break;
                case Language.español:
                    spell = new Hunspell("spanish.aff", "spanish.dic");
                    break;
                case Language.English:
                    spell = new Hunspell("english.aff", "english.dic");
                    break;
                default:
                    break;
            }

            suggestions = spell.Suggest(text);
            return suggestions;
        }

        public static bool EnviarCorreo(emailSettings eSettings)
        {
            return EnviarMails.EnviarMails.EnviarMail(eSettings.From, eSettings.To, eSettings.Subject, eSettings.Body, eSettings.smtp, eSettings.Password, eSettings.Host, eSettings.AttachedFiles, eSettings.Important);
        }

        /// <summary>
        /// configurar propiedades envio Email
        /// </summary>
        public class emailSettings
        {
            public string From { get; set; }
            public string To { get; set; }
            public string Subject { get; set; }
            public string Body { get; set; }
            public string smtp { get; set; }
            public string Password { get; set; }
            public string Host { get; set; }
            public string AttachedFiles { get; set; }
            public bool Important { get; set; }
        }

    }

    public static class SosSecurity
    {
        /// <summary>
        /// return Hash as string
        /// </summary>
        /// <param name="input">input string</param>
        /// <returns>hash string</returns>
        public static string CalculateMD5Hash(string input)
        {

            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }
    }
}